package view
{
	import com.bit101.components.Text;
	
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	
	import models.ModelLocator;
	
	import utils.DrawUtils;
	
	public class EditSpace extends Sprite
	{
		private var dic:Object;
		public var currentEdit:EditSpaceItem;
		private var btnSp:Sprite;
		public var listItem:ListItem;
		public var shape:Shape;
		public var maskShape:Shape;
		public var maskList:Array;
		public var maskContent:Sprite;
		public function EditSpace()
		{
			dic = new Object;
			this.graphics.lineStyle(1,0x999999);
			this.graphics.drawRect(0,0,444,444);
			
			btnSp = new Sprite;
			this.addChild(btnSp);
			
			addRedoBtn();
			addTxtBtn();
			addFileBtn();
			
			shape = new Shape;
			btnSp.addChild(shape);
			maskShape = new Shape;
			maskContent = new Sprite;
			this.addChild(maskContent);
			//maskContent.visible = false;
			maskList = new Array;
			
			var txt:TextField = new TextField;
			txt.width = 400;
			txt.height = 22;
			txt.htmlText = "<font color='#656565' size='12'>备注：虚线内为可打印尺寸，虚线不会被打印在衣服上</font>"
			txt.y = 450;
			txt.selectable = false;
			this.addChild(txt);
		}
		public function drawRct(obj:Object):void{
			shape.graphics.clear();
			shape.graphics.lineStyle(1,0x999999);
			//shape.graphics.drawRect(obj.x,obj.y,obj.w,obj.h);
			DrawUtils.drawDashed(shape.graphics,new Point(obj.x,obj.y),new Point(obj.x+obj.w,obj.y),3,3);
			DrawUtils.drawDashed(shape.graphics,new Point(obj.x+obj.w,obj.y),new Point(obj.x+obj.w,obj.y+obj.h),3,3);
			DrawUtils.drawDashed(shape.graphics,new Point(obj.x+obj.w,obj.y+obj.h),new Point(obj.x,obj.y+obj.h),3,3);
			DrawUtils.drawDashed(shape.graphics,new Point(obj.x,obj.y+obj.h),new Point(obj.x,obj.y),3,3);
			shape.graphics.endFill();
			
			maskShape.graphics.clear();
			maskShape.graphics.beginFill(0x666666,0);
			maskShape.graphics.drawRect(0,0,obj.w,obj.h);
			maskShape.graphics.endFill();
			maskShape.x = obj.x;
			maskShape.y = obj.y;
			
			for(var i:int;i<maskList.length;i++){
				var m:Shape = maskList[i];
				m.graphics.clear();
				m.graphics.beginFill(0x666666,0);
				m.graphics.drawRect(0,0,obj.w,obj.h);
				m.graphics.endFill();
				m.x = obj.x;
				m.y = obj.y;
			}
		}
		public function getMaksShape():Shape{
			var s:Shape = new Shape;
			s.graphics.clear();
			s.graphics.beginFill(0x666666,0);
			s.graphics.drawRect(0,0,maskShape.width,maskShape.height);
			s.graphics.endFill();
			s.x = maskShape.x;
			s.y = maskShape.y;
			maskContent.addChild(s);
			return s;
		}
		private function addRedoBtn():void{
			var sp:Sprite = new Sprite;
			btnSp.addChild(sp);
			var txt:TextField = new TextField;
			txt.width = 43;
			txt.height = 22;
			txt.y = 3;
			txt.htmlText = "<p align='center'><font color='#ffffff' size='15'><a href='event:myevent'>重做</a></font></p>";
			txt.addEventListener(MouseEvent.CLICK,onRedo);
			
			sp.x = 1;
			sp.y = 2;
			sp.addChild(txt);
			sp.graphics.beginFill(0x000000,0.3);
			sp.graphics.drawRect(0,0,43,26);
			sp.graphics.endFill();
		}
		private function onRedo(event:MouseEvent):void{
			var alert:AlertShow = Main(this.parent).alert;
			alert.setInfo("重做将删除所有面已创建文字和图片你确定要重做吗？",redosure);
			
			this.parent.addChild(alert);
			alert.x = 200;
			alert.y = 200;
		}
		private function redosure():void{
			if(currentEdit)
				currentEdit.removeAll();
		}
		private function addTxtBtn():void{
			var sp:Sprite = new Sprite;
			btnSp.addChild(sp);
			var txt:TextField = new TextField;
			txt.width = 65;
			txt.height = 22;
			txt.y = 3;
			txt.htmlText = "<p align='center'><font color='#ffffff' size='15'><a href='event:myevent'>添加文字</a></font></p>";
			txt.addEventListener(MouseEvent.CLICK,onTxtBtn);
			
			sp.x = 294;
			sp.y = 2;
			sp.addChild(txt);
			sp.graphics.beginFill(0x000000,0.3);
			sp.graphics.drawRect(0,0,65,26);
			sp.graphics.endFill();
		}
		private function onTxtBtn(event:MouseEvent):void{
			if(listItem.currentEdit){
				listItem.currentEdit.onTxtClick();
			}
		}
		private function addFileBtn():void{
			var sp:Sprite = new Sprite;
			btnSp.addChild(sp);
			var txt:TextField = new TextField;
			txt.width = 65;
			txt.height = 22;
			txt.y = 3;
			txt.htmlText = "<p align='center'><font color='#ffffff' size='15'><a href='event:myevent'>上传图片</a></font></p>";
			txt.addEventListener(MouseEvent.CLICK,onFileBtn);
			
			sp.x = 370;
			sp.y = 2;
			sp.addChild(txt);
			sp.graphics.beginFill(0x000000,0.3);
			sp.graphics.drawRect(0,0,65,26);
			sp.graphics.endFill();
		}
		private function onFileBtn(event:MouseEvent):void{
			if(listItem.currentEdit){
				listItem.currentEdit.getFile();
			}
		}
		public function clearType():void{
			for each(var edit:EditSpaceItem in dic){
				edit.showedit = false;
			}
		}
		public function setMarkColor(mark:String,color:String,showedit:int = -1,data:Object=null):void{
			if(mark==null || color == null){
				return;
			}
			var edit:EditSpaceItem = new EditSpaceItem
			if(dic.hasOwnProperty(mark)){
				edit = dic[mark];
			}else{
				edit = new EditSpaceItem
				dic[mark] = edit;
			}
			edit.setMarkColor(mark,color);
			if(showedit != -1){
				edit.showedit = showedit;
			}
			if(currentEdit != edit){
				if(currentEdit != null && currentEdit.parent != null){
					currentEdit.parent.removeChild(currentEdit);
					if(Main(this.parent).editTxt){
						Main(this.parent).editTxt.visible = false;
					}
				}
				this.addChildAt(edit,0);
				currentEdit = edit;
				//ModelLocator.getInstance().currentdata = edit.data;
			}
			edit.data = data;
			if(!currentEdit){
				currentEdit = edit;
			}
			ModelLocator.getInstance().currentdata = currentEdit.data;
		}
		public function addTxt():Console{
			if(currentEdit != null){
				var ms:Shape = getMaksShape();
				maskList.push(ms);
				return currentEdit.addTxt(ms);
			}
			return null;
		}
		public function addBitmap(bitmap:Bitmap,url:Object,scale:Number,lev:int):Console{
			if(currentEdit != null){
				var ms:Shape = getMaksShape();
				maskList.push(ms);
				return currentEdit.addBitmap(bitmap,ms,url,scale,lev);
			}
			return null;
		}
		public function getAllInfo():String{
			var str:String = "";
			for each(var obj:EditSpaceItem in dic){
				if(obj.showedit && obj.data){
					str += obj.getAllInfoStr() +"]";
				}
			}
			str = str.substr(0,str.length-1);
			return str;
		}
		public function getAllInfoImg():Array{
			var o:Object;
			var ary:Array = new Array;
			for each(var obj:EditSpaceItem in dic){
				if(obj.showedit && obj.data){
					o = new Object;
					o.mark = obj.data.mark;
					o.name = obj.data.name;
					o.byte = obj.getImage();
					ary.push(o);
				}
			}
			return ary;
		}
		public function clear():void{
			if(currentEdit != null && currentEdit.parent != null){
				currentEdit.parent.removeChild(currentEdit);
				if(Main(this.parent).editTxt){
					Main(this.parent).editTxt.visible = false;
				}
			}
			dic = new Object;
		}
		
	}
}